package view;

import controler.EdgeItemControler;
import controler.MainPanelControler;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.Stroke;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JLabel;

/**
 *
 * @author borysfan
 */
public class MainPanel extends javax.swing.JPanel  {
    
    private MainPanelControler mainPanelControler;
    private NextPrevPanel nextPrevPanel = null;

    public MainPanel(MainPanelControler mpc) {
        mainPanelControler = mpc;
        initComponents();
        addMouseListener(new MyMouseListener());
        setLayout(null);
       

    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        PlusBtn = new javax.swing.JButton();
        rightBtn = new javax.swing.JButton();
        leftBtn = new javax.swing.JButton();
        upBtn = new javax.swing.JButton();
        downBtn = new javax.swing.JButton();

        setFocusable(false);
        setLayout(null);

        PlusBtn.setText("Rozwiązania");
        PlusBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                PlusBtnActionPerformed(evt);
            }
        });
        add(PlusBtn);
        PlusBtn.setBounds(0, 0, 120, 23);

        rightBtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/view/right.png"))); // NOI18N
        rightBtn.setBorder(null);
        rightBtn.setContentAreaFilled(false);
        rightBtn.setFocusPainted(false);
        rightBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rightBtnActionPerformed(evt);
            }
        });
        add(rightBtn);
        rightBtn.setBounds(90, 500, 40, 40);

        leftBtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/view/left.png"))); // NOI18N
        leftBtn.setBorder(null);
        leftBtn.setBorderPainted(false);
        leftBtn.setContentAreaFilled(false);
        leftBtn.setFocusPainted(false);
        add(leftBtn);
        leftBtn.setBounds(10, 500, 40, 40);

        upBtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/view/up.png"))); // NOI18N
        upBtn.setBorder(null);
        upBtn.setBorderPainted(false);
        upBtn.setContentAreaFilled(false);
        upBtn.setFocusPainted(false);
        add(upBtn);
        upBtn.setBounds(50, 460, 40, 40);

        downBtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/view/down.png"))); // NOI18N
        downBtn.setBorder(null);
        downBtn.setBorderPainted(false);
        downBtn.setContentAreaFilled(false);
        downBtn.setFocusPainted(false);
        add(downBtn);
        downBtn.setBounds(50, 540, 40, 40);
    }// </editor-fold>//GEN-END:initComponents

    private void PlusBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PlusBtnActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_PlusBtnActionPerformed

    private void rightBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rightBtnActionPerformed
   
    }//GEN-LAST:event_rightBtnActionPerformed

   
    @Override
    public void paint(Graphics g){
        super.paint(g);
        Graphics2D g2 = (Graphics2D)g;
        
          Stroke stroke = new BasicStroke(3);
        g2.setStroke(stroke);
        ArrayList<EdgeItemControler> edges = mainPanelControler.getEdgesList();
        for(int i=0;i<edges.size();i++){
            g2.setColor(mainPanelControler.getDefColor(edges.get(i)));
            NodeItem begin = edges.get(i).getBeginNode().getView();
            NodeItem end = edges.get(i).getEndNode().getView();
            if (mainPanelControler.showIcons()==true){
            int x = (begin.getX()+end.getX())/2;
            int y = (begin.getY()+end.getY())/2;
            if (edges.get(i).getModel().isLock()==true){
            g2.drawLine(begin.getX()+20, begin.getY()+20,end.getX()+20,end.getY()+20);
            g2.drawImage(mainPanelControler.getBlockImage(), null, x+7, y+7);
            }
            else{
            g2.drawLine(begin.getX()+20, begin.getY()+20,end.getX()+20,end.getY()+20);
            g2.drawImage(mainPanelControler.getWeatherImage(edges.get(i)), null, x+9, y+9);
            g2.drawImage(mainPanelControler.getRoadImage(edges.get(i)), null, x-16, y+9);
            if(edges.get(i).getModel().getTraffic()==0)
                g2.drawImage(mainPanelControler.getTrafficImage(), null, x+40, y+11);
             if(edges.get(i).getModel().getTraffic()==1){
                 g2.drawImage(mainPanelControler.getTrafficImage(), null, x+40, y+7);
                 g2.drawImage(mainPanelControler.getTrafficImage(), null, x+40, y+15);}
             if(edges.get(i).getModel().getTraffic()==2){
                 g2.drawImage(mainPanelControler.getTrafficImage(), null, x+40, y+3);
                 g2.drawImage(mainPanelControler.getTrafficImage(), null, x+40, y+11);
                 g2.drawImage(mainPanelControler.getTrafficImage(), null, x+40, y+19);
             }
            }}
            else{
            g2.drawLine(begin.getX()+20, begin.getY()+20,end.getX()+20,end.getY()+20);
            }
        }
        
    }

    public void printOnlyGraph(Graphics g){
        Graphics2D g2 = (Graphics2D)g;
       
        Stroke stroke = new BasicStroke(3);
        g2.setStroke(stroke);
        ArrayList<EdgeItemControler> edges = mainPanelControler.getEdgesList();
        Component [] components = getComponents();
        for(int i=0;i<components.length;i++){
            if(components[i] instanceof NodeItem){
                NodeItem tmp = (NodeItem) components[i];
                tmp.paintToFile(g2);
            }
        }
        for(int i=0;i<edges.size();i++){
            g2.setColor(mainPanelControler.getDefColor(edges.get(i)));
            NodeItem begin = edges.get(i).getBeginNode().getView();
            NodeItem end = edges.get(i).getEndNode().getView();
            
            if (mainPanelControler.showIcons()==true){
            int x = (begin.getX()+end.getX())/2;
            int y = (begin.getY()+end.getY())/2;
            if (edges.get(i).getModel().isLock()==true){
            g2.drawLine(begin.getX()+20, begin.getY()+20,end.getX()+20,end.getY()+20);
            g2.drawImage(mainPanelControler.getBlockImage(), null, x+7, y+7);
            }
            else{
            g2.drawLine(begin.getX()+20, begin.getY()+20,end.getX()+20,end.getY()+20);
            g2.drawImage(mainPanelControler.getWeatherImage(edges.get(i)), null, x+9, y+9);
            g2.drawImage(mainPanelControler.getRoadImage(edges.get(i)), null, x-16, y+9);
            if(edges.get(i).getModel().getTraffic()==0)
                g2.drawImage(mainPanelControler.getTrafficImage(), null, x+40, y+11);
             if(edges.get(i).getModel().getTraffic()==1){
                 g2.drawImage(mainPanelControler.getTrafficImage(), null, x+40, y+7);
                 g2.drawImage(mainPanelControler.getTrafficImage(), null, x+40, y+15);}
             if(edges.get(i).getModel().getTraffic()==2){
                 g2.drawImage(mainPanelControler.getTrafficImage(), null, x+40, y+3);
                 g2.drawImage(mainPanelControler.getTrafficImage(), null, x+40, y+11);
                 g2.drawImage(mainPanelControler.getTrafficImage(), null, x+40, y+19);
             }
            }}
            else{
            g2.drawLine(begin.getX()+20, begin.getY()+20,end.getX()+20,end.getY()+20);
            }
        }
        mainPanelControler.resetColor();
    }

    public void addNextPrevPanel(NextPrevPanel panel){
        add(panel);
        nextPrevPanel = panel;
        nextPrevPanel.setBounds(150, 510, 380, 45);
        nextPrevPanel.setVisible(false);
    }

    
    public void setPlusBtnVisible(boolean b) {
        PlusBtn.setVisible(b);
    }

    public void setPlusBtnAction(ActionListener listener) {
        PlusBtn.addActionListener(listener);
    }

    public void setRightBtnAction(ActionListener listener){
        rightBtn.addActionListener(listener);
        System.out.println("prawo");
        repaint();
    }

    public void setLeftBtnAction(ActionListener listener){
        leftBtn.addActionListener(listener);
        repaint();
    }

    public void setUpBtnAction(ActionListener listener){
        upBtn.addActionListener(listener);
        repaint();
    }

    public void setDownBtnAction(ActionListener listener){
        downBtn.addActionListener(listener);
        repaint();
    }

   public MainPanelControler getControler(){
       return mainPanelControler;
   }

    private class MyMouseListener implements MouseListener{
        public void mousePressed(MouseEvent e)        {
            
        mainPanelControler.point(e.getPoint());
        }

        public void mouseClicked(MouseEvent e) {
        mainPanelControler.point(e.getPoint());
        }

        public void mouseReleased(MouseEvent e) {

        }

        public void mouseEntered(MouseEvent e) {

        }

        public void mouseExited(MouseEvent e) {

        }
    }

    public void setPrevNextPanelVisible(boolean visible){
        nextPrevPanel.setVisible(visible);
    }
   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton PlusBtn;
    private javax.swing.JButton downBtn;
    private javax.swing.JButton leftBtn;
    private javax.swing.JButton rightBtn;
    private javax.swing.JButton upBtn;
    // End of variables declaration//GEN-END:variables

}
